<?php

namespace App\Model\Backend;

use App\Http\Requests\Request;
use Illuminate\Database\Eloquent\Model;
use Chumper\Zipper\Zipper;

class ItemTasksAccessoryModel extends Model
{
    protected $table = 'items_tasks_accessory';
    protected $primaryKey = 'id';

    public function taskInfo($item_id,$task_id,$tag){
        $arr=array();
        $a=array();
        $b=array();
        $task=new ItemTasksModel();
        $log=new TaskLogModel();
        $member=new Member();
        $logs=$log->where(['item_task_id'=>$task_id])->orderBy('id','ASC')->get();
        $itemInfo=$task->find($task_id);
        $data=['item_id'=>$item_id,'item_task_id'=>$task_id];
        if(!$tag){

            $data['status']=0;
        }

        $accessorys=$this->where($data)->orderBy('created_at','ASC')->get();

        foreach($logs as $k=>$v){

            $b[$k]=$v;
            $b[$k]['author']=$member->find($v['member_id'])['username']?$member->find($v['member_id'])['username']: "";
        }


        $kk=0;
        foreach($accessorys as $i=>$y){

            $path=explode(',',trim($y['path'],','));
            foreach($path as $k=>$v){
                $a[$kk]['id']=$accessorys[$i]['id'];
                $a[$kk]['item_id']=$accessorys[$i]['item_id'];
                $a[$kk]['item_task_id']=$accessorys[$i]['item_task_id'];
                $a[$kk]['path']=$v;
                $a[$kk]['author']=$accessorys[$i]['author'];
                $a[$kk]['comment']=$accessorys[$i]['comment'];
                $a[$kk]['status']=$accessorys[$i]['status'];
                $a[$kk]['created_at']=$accessorys[$i]['created_at'];
                $kk++;
            }
        }


        $arr['name']=$itemInfo['task_name'];
        $arr['deadline']=$itemInfo['task_deadline'];
        if($itemInfo['task_status']==10&&strtotime($itemInfo['task_deadline'])-time()>0||$itemInfo['task_status']>=20&&$itemInfo['task_status']<30&&strtotime($itemInfo['updated_at'])+3600-time()>0){
            $arr['is_timeout']=0;
        }else if($itemInfo['task_status']==10&&strtotime($itemInfo['task_deadline'])-time()<0||$itemInfo['task_status']>=20&&$itemInfo['task_status']<30&&strtotime($itemInfo['updated_at'])+3600-time()<0){
            $arr['is_timeout']=1;
        }
        $arr['accessorys']=$a;
        $arr['logs']=$b;
        return $arr;
    }


    public function get_path( $task_id,$item_id){


//        item_id:133
//        task_id:1881
//点击传过来的阶段id拿到项目任务表的项目阶段id
        $stage_id=ItemTasksModel::find($task_id)['task_id'];//154


        if($stage_id == 151){ //如果项目阶段id是151(也就是CAD阶段)
            $stage_id = [149];
            $taskInfo = $this->allStages($item_id,$task_id,$stage_id);
        }elseif( $stage_id==152 ){ //如果项目阶段id是152(也就是彩屏阶段)
            $stage_id = [151];
            $taskInfo = $this->allStages($item_id,$task_id,$stage_id);
        }elseif ($stage_id==153){//如果项目阶段id是153(也就是文案阶段)
            $stage_id = [150,152];
            $taskInfo = $this->allStages($item_id,$task_id,$stage_id);
        }elseif ($stage_id==154){//如果项目阶段id是154(也就是解说1阶段)
            $stage_id = [150,152,153];
            $taskInfo = $this->allStages($item_id,$task_id,$stage_id);
        }elseif( $stage_id==155 ){//如果项目阶段id是155(也就是估算阶段)
            $stage_id = [151,152];
            $taskInfo = $this->allStages($item_id,$task_id,$stage_id);
        }elseif( $stage_id==156 ){//如果项目阶段id是156(也就是su阶段)
            $stage_id = [150,151,152];
            $taskInfo = $this->allStages($item_id,$task_id,$stage_id);
        }elseif( $stage_id==157 ){//如果项目阶段id是157(也就是lu阶段)
            $stage_id = [156,151,152];
            $taskInfo = $this->allStages($item_id,$task_id,$stage_id);
        }elseif( $stage_id==158 ){//如果项目阶段id是158(也就是文案2阶段)
            $stage_id = [157,151,152];
            $taskInfo = $this->allStages($item_id,$task_id,$stage_id);
        }elseif( $stage_id==159 ){//如果项目阶段id是158(也就是解说2阶段)
            $stage_id = [158,157,151,152];
            $taskInfo = $this->allStages($item_id,$task_id,$stage_id);
        }elseif( $stage_id==160 ){//如果项目阶段id是160(也就是概算阶段)
            $stage_id = [151,156,157];
            $taskInfo = $this->allStages($item_id,$task_id,$stage_id);
        }elseif( $stage_id == 161||$stage_id==162||$stage_id==163 ){//如果项目阶段id是161(也就是园建施工图阶段)
            $stage_id = [151,152,156,157];
            $taskInfo = $this->allStages($item_id,$task_id,$stage_id);
        }elseif( $stage_id==164||$stage_id == 165 ){//如果项目阶段id是160(也就是概算阶段)
            $stage_id = [161,162,163];
            $taskInfo = $this->allStages($item_id,$task_id,$stage_id);
        }else{
            $taskInfo=0;
        }

        return $taskInfo;
    }

    public function  allStages($item_id,$task_id,$stage_id){
        //就拿到该项目阶段对应的CAD,文案和意向的id
        $require_id = ItemTasksModel::whereIn('task_id',$stage_id)->where('item_id','=',$item_id)->get();
        //拿到项目名
        $item_name=ItemModel::find($item_id)['item_name'];
        //创建属于该项目阶段的文件夹
        $task_name = ItemTasksModel::find($task_id)['task_name'];
        $dir="uploads/".$item_name."/".$task_name;
        $dir=iconv('utf-8', 'gbk',$dir);
        //创建文件夹
        if(!file_exists($dir)){
            @mkdir($dir,0777,true);//解说文件夹创建
        }

        //拿到该项目对应的CAD,文案和意向的上传文件id
        foreach ($require_id as $k=>$v){
            $require_files_id[] = $v->id;//[1877,1878,1879]
        }

    if($require_files_id){
        $require_files = ItemTasksAccessoryModel::whereIn('item_task_id',$require_files_id)
            ->where('item_id','=',$item_id)
            ->where('status','=',1)
            ->get()
            ->toArray();

        //三个需求ID对应三个已上传并且通过的文件
      
        if( count($require_files)==count($require_files_id) ){

            //任务ID-----0-1877,1-1878,2-1879
            $item_name = iconv('utf-8', 'gbk', $item_name);
            //在外层定义id,自增
            $id=0;
            //dd($require_files);
            foreach ( $require_files as $k=>$v ){

                //对应的任务名
                $name[$k] = ItemTasksModel::find($v['item_task_id'])['task_name'];

                //转码
                $name[$k] = iconv('utf-8', 'gbk', $name[$k]);

                //所有文件内容
                $all_pass_file = explode(',', trim($v['path'],','));
                foreach( $all_pass_file as $o=>$p ){
                    $pathInfo = iconv('utf-8', 'gbk', trim($p,','));
                    $file[$id] = public_path()."/uploads/$item_name/$name[$k]/$pathInfo";
                    $id++;
                }

            }

            //循环文件

            foreach( $file as $k=>$v ){
                $zip[] = glob($v);
            }


            //保存路径

            $task_name = iconv('utf-8','gbk',$task_name);
            $url = public_path()."/uploads/$item_name/$task_name/$task_name.zip";

            if( !file_exists($url) ){
                $zipper = new Zipper;
                //打包保存
                $zipper->make($url)->add(
                    $zip
                );
            }

            $path = $task_id;
        }else{
            $path = 0;
        }

    }else{
        $path=0;
    }

        return $path;
    }


}
